package com.qing.library.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by DaiQing.Zha on 2016/6/7 0007.
 * Email:1642674371@qq.com
 * Description:核心工具类
 */
public class CoreUtil {

    public static ArrayList<Activity> ALL_ACTIVITY=new ArrayList<Activity>();

    /**
     * @Description: 添加Activity到列表中
     * @param activity
     */
    public static void addAppActivity(Activity activity){
        if(!ALL_ACTIVITY.contains(activity)){
            ALL_ACTIVITY.add(activity);
        }
    }
    /**
     * @Description: 从列表移除Activity
     * @param activity
     */
    public static void removeAppActivity(Activity activity){
        if(ALL_ACTIVITY.contains(activity)){
            ALL_ACTIVITY.remove(activity);
        }
    }
    /**
     * @Description: 退出应用程序
     */
    public static void exitApp(Context context){
        L.e("--- 销毁 Activity size--->>:" + ALL_ACTIVITY.size());
        for (Activity ac : ALL_ACTIVITY) {
            if(!ac.isFinishing()){
                ac.finish();
            }
        }
        ALL_ACTIVITY.clear();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 打印栈中所有的activity
     */
    public static void printAllActivity(){
        int i = 0;
        for (Activity ac:ALL_ACTIVITY){
            i++;
            L.e("activity_" + i + " = " + ac);
        }
    }

    /**
     * 结束所有activity
     */
    public static void killAllActivity() {
        Stack<Activity> tempStack = new Stack<>();// 必须用一个临时的stack过渡，ALL_ACTIVITY，那么就会在for循环中删除stack,就会出错
        for (int i = 0; i < ALL_ACTIVITY.size(); i++) {
            if (null != ALL_ACTIVITY.get(i)) {
                tempStack.add(ALL_ACTIVITY.get(i));
            }
        }
        for (int i = 0; i < tempStack.size(); i++) {
            if (null != tempStack.get(i)) {
                tempStack.get(i).finish();
            }
        }
        ALL_ACTIVITY.clear();
        tempStack.clear();
    }

    /**
     * 显示进度对话框
     * @param context
     * @param message
     */
    public static ProgressDialog showProgressDialog(Context context,String message){
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage(message);
        dialog.show();
        return dialog;
    }

    /**
     * 获取栈顶Activity
     */
    public static Activity getTopActivity() {

        if (ALL_ACTIVITY != null && ALL_ACTIVITY.size() > 0){
            Activity activity = ALL_ACTIVITY.get(ALL_ACTIVITY.size() - 1);
            return activity;
        }
        return null;
    }

    /**
     * 获取栈底Activity
     */
    public static Activity getBottomActivity() {
        if (ALL_ACTIVITY != null && ALL_ACTIVITY.size() > 0){

            Activity activity = ALL_ACTIVITY.get(0);
            return activity;
        }
        return null;
    }

    /**
     * 结束栈顶Activity
     */
    public static void killTopActivity() {
        if (ALL_ACTIVITY != null && ALL_ACTIVITY.size() > 0){

            Activity activity = ALL_ACTIVITY.get(ALL_ACTIVITY.size() - 1);
            removeAppActivity(activity);
        }
    }

    /**
     * 判断应用是否在前台显示
     * @param context
     * @param $packageName
     * @return
     */
    public static boolean isTopActivity(Context context,String $packageName)
    {
        //_context是一个保存的上下文
        ActivityManager __am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> __list = __am.getRunningAppProcesses();
        if(__list.size() == 0) return false;
        for(ActivityManager.RunningAppProcessInfo __process:__list)
        {
            if(__process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                    __process.processName.equals($packageName))
            {
                return true;
            }
        }
        return false;
    }
}
